package com.ruoyi.web.controller.module;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.OkHttpClientUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.Parameter;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.IParameterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 配置参数表Controller
 *
 * @author ruoyi
 * @date 2022-01-04
 */
@Validated
@Api(value = "配置参数表控制器", tags = {"配置参数表管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/parameter")
public class ParameterController extends BaseController {

    private final IParameterService parameterService;


	@Value("${ruoyi.apiUrl}")
	private String apiUrl;

    /**
     * 查询配置参数表列表
     */
    @ApiOperation("查询配置参数表列表")
    @PreAuthorize("@ss.hasPermi('module:parameter:list')")
    @GetMapping("/list")
    public TableDataInfo<Parameter> list(@Validated Parameter parameter) {
        QueryWrapper<Parameter> qw = new QueryWrapper<>();

        TableDataInfo<Parameter> td = PageUtils.buildDataInfo(parameterService.page(PageUtils.buildPage(),qw));
        List<Parameter> list = td.getRows();
        return td;
    }

    /**
     * 导出配置参数表列表
     */
    @ApiOperation("导出配置参数表列表")
    @PreAuthorize("@ss.hasPermi('module:parameter:export')")
    @Log(title = "配置参数表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Parameter> export(@Validated Parameter parameter) {
        List<Parameter> list = parameterService.list();
        ExcelUtil<Parameter> util = new ExcelUtil<Parameter>(Parameter.class);
        return util.exportExcel(list, "配置参数表");
    }

    /**
     * 获取配置参数表详细信息
     */
    @ApiOperation("获取配置参数表详细信息")
    @PreAuthorize("@ss.hasPermi('module:parameter:query')")
    @GetMapping("/{id}")
    public AjaxResult<Parameter> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Integer id) {
        Parameter parameter = parameterService.getParameterById(id);
        return AjaxResult.success(parameter);
    }

    /**
     * 新增配置参数表
     */
    @ApiOperation("新增配置参数表")
    @PreAuthorize("@ss.hasPermi('module:parameter:add')")
    @Log(title = "配置参数表", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody Parameter parameter) {
        return toAjax(parameterService.save(parameter) ? 1 : 0);
    }

    /**
     * 修改配置参数表
     */
    @ApiOperation("修改配置参数表")
    @PreAuthorize("@ss.hasPermi('module:parameter:edit')")
    @Log(title = "配置参数表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
	@CacheEvict(value = "parameter", allEntries = true)//清除缓存
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody Parameter parameter) {
		OkHttpClientUtil.getInstance().getData(apiUrl + "parameter/reset/1");
        return toAjax(parameterService.updateById(parameter) ? 1 : 0);
    }

    /**
     * 删除配置参数表
     */
    @ApiOperation("删除配置参数表")
    @PreAuthorize("@ss.hasPermi('module:parameter:remove')")
    @Log(title = "配置参数表" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
	@CacheEvict(value = "parameter", allEntries = true)//清除缓存
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(parameterService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


	/**
	 * 删除配置参数表
	 */
	@ApiOperation("删除配置参数表")
	@PreAuthorize("@ss.hasPermi('module:parameter:remove')")
	@Log(title = "配置参数表" , businessType = BusinessType.DELETE)
	@GetMapping("/clearRedis")
	@CacheEvict(value = "parameter", allEntries = true)//清除缓存
	public AjaxResult<Void> clearRedis() {
		OkHttpClientUtil.getInstance().getData(apiUrl + "parameter/reset/1");
		return toAjax(1);
	}


}
